package com.weiwu.weiwustore.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiwu.weiwustore.entity.Store;
import com.weiwu.weiwustore.entity.User;
import com.weiwu.weiwustore.mapper.StoreMapper;
import com.weiwu.weiwustore.mapper.UserMapper;
import com.weiwu.weiwustore.pojo.SelectItem;
import com.weiwu.weiwustore.pojo.StoreListVo;
import com.weiwu.weiwustore.service.StoreService;
import com.weiwu.weiwustore.service.UserService;
import com.weiwu.weiwustore.utils.ObjectFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.weiwu.weiwustore.utils.ObjectFormatUtil.toInteger;

@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {
    @Autowired
    StoreMapper storeMapper;

    @Override
    public List<StoreListVo> getstorelist(Map<String, Object> map) {
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
        // 根据搜索条件
        Integer size = 10, page = 1;
        IPage pageP = new Page(page, size);
        List<StoreListVo> res = new ArrayList<>();
        if (map.containsKey("size")) {
            size = toInteger(map.get("size"));
        }
        if (map.containsKey("page")) {
            page = ObjectFormatUtil.toInteger(map.get("page"));
        }
        pageP.setSize(size);
        pageP.setCurrent(page);
        IPage storelist = storeMapper.selectPage(pageP, wrapper);
        return singlePackStore(storelist.getRecords());
    }

    @Override
    public Integer findCount() {
        return storeMapper.selectCount(null);
    }

    @Override
    public Integer storeadd(Map<String, Object> map) {
        Store store = new Store(String.valueOf(map.get("sname")), String.valueOf(map.get("address")), String.valueOf(map.get("introduce")));
        return storeMapper.insert(store);
    }

    @Override
    public Integer storesave(Map<String, Object> map) {
        Integer id = toInteger(map.get("id"));
        Store store = new Store(String.valueOf(map.get("sname")), String.valueOf(map.get("address")), String.valueOf(map.get("introduce")));
        store.setId(id);
        return storeMapper.updateById(store);
    }

    @Override
    public List<SelectItem> findDepositoryItem() {
        List<SelectItem> res = new ArrayList<>();
        for (Store d : storeMapper.selectList(null)) {
            res.add(new SelectItem(d.getId().toString(), d.getSname()));
        }
        return res;
    }

    private List<StoreListVo> singlePackStore(List<Store> list) {
        List<StoreListVo> res = new ArrayList<>();
        if (list.size() == 0) {
            return null;
        }
        for (Store s : list) {
            res.add(new StoreListVo(s.getId(), s.getSname(), s.getAddress(), s.getIntroduce(), s.getCreatetime(), s.getUpdatetime()));
        }
        return res;
    }
}
